package com.ruoyi.system.service.impl;

import com.ruoyi.system.domain.UserFollow;
import com.ruoyi.system.mapper.UserFollowMapper;
import com.ruoyi.system.service.IUserFollowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.domain.entity.SysUser;
import java.util.List;

/**
 * 用户关注Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-28
 */
@Service
public class UserFollowServiceImpl implements IUserFollowService
{
    @Autowired
    private UserFollowMapper userFollowMapper;

    @Override
    public boolean toggleFollow(Long userId, Long followedUserId)
    {
        UserFollow follow = userFollowMapper.selectUserFollow(userId, followedUserId);
        if (follow == null)
        {
            UserFollow newFollow = new UserFollow();
            newFollow.setUserId(userId);
            newFollow.setFollowedUserId(followedUserId);
            userFollowMapper.insertUserFollow(newFollow);
            return true; // 关注成功
        }
        else
        {
            userFollowMapper.deleteUserFollow(userId, followedUserId);
            return false; // 取消关注
        }
    }

    @Override
    public boolean checkUserFollowed(Long userId, Long followedUserId)
    {
        return userFollowMapper.selectUserFollow(userId, followedUserId) != null;
    }

    @Override
    public int countFollow(Long userId) {
        return userFollowMapper.countFollow(userId);
    }

    @Override
    public int countFans(Long userId) {
        return userFollowMapper.countFans(userId);
    }

    @Override
    public List<SysUser> selectFollowsByUserId(Long userId) {
        return userFollowMapper.selectFollowsByUserId(userId);
    }

    @Override
    public List<SysUser> selectFansByUserId(Long userId) {
        return userFollowMapper.selectFansByUserId(userId);
    }
} 